package org.example.queueadmin.task;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/tasks")
@RequiredArgsConstructor
public class TaskController {
  private final TaskService service;

  @GetMapping
  public List<TransferTask> list() { return service.list(); }

  @PostMapping
  public TransferTask createOrUpdate(@RequestBody TransferTask t) { return service.createOrUpdate(t); }

  @DeleteMapping("/{id}")
  public ResponseEntity<Void> delete(@PathVariable("id") Long id) { service.delete(id); return ResponseEntity.ok().build(); }

  @PostMapping("/{id}/start")
  public ResponseEntity<Void> start(@PathVariable("id") Long id) { service.start(id); return ResponseEntity.ok().build(); }

  @PostMapping("/{id}/stop")
  public ResponseEntity<Void> stop(@PathVariable("id") Long id) { service.stop(id); return ResponseEntity.ok().build(); }

  @PostMapping("/{id}/restart")
  public ResponseEntity<Void> restart(@PathVariable("id") Long id) { service.restart(id); return ResponseEntity.ok().build(); }

  @GetMapping("/{id}/status")
  public String status(@PathVariable("id") Long id) { return service.status(id); }

  @PostMapping("/{id}/clear-error")
  public ResponseEntity<Void> clearError(@PathVariable("id") Long id) {
    service.clearError(id);
    return ResponseEntity.ok().build();
  }

  @GetMapping("/{id}")
  public ResponseEntity<TransferTask> getTask(@PathVariable("id") Long id) {
    return ResponseEntity.ok(service.findById(id));
  }
}

